package com.realtyManage.springboot.Dao;

import com.realtyManage.springboot.Model.Do.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ArticleRepository extends JpaRepository<Article,Integer> {

    Page<Article> findByTypeAndState(Pageable pageable, Integer type, int state);

    @Modifying
    @Transactional
    @Query("update Article a set " +
            "a.title =  CASE WHEN :#{#article.title} IS NULL THEN a.title ELSE :#{#article.title} END ," +
            "a.pic =  CASE WHEN :#{#article.pic} IS NULL THEN a.pic ELSE :#{#article.pic} END ," +
            "a.content =  CASE WHEN :#{#article.content} IS NULL THEN a.content ELSE :#{#article.content} END ," +
            "a.addTime =  CASE WHEN :#{#article.addTime} IS NULL THEN a.addTime ELSE :#{#article.addTime} END ," +
            "a.showTime =  CASE WHEN :#{#article.showTime} IS NULL THEN a.showTime ELSE :#{#article.showTime} END ," +
            "a.isExhibition =  CASE WHEN :#{#article.isExhibition} IS NULL THEN a.isExhibition ELSE :#{#article.isExhibition} END ," +
            "a.state =  CASE WHEN :#{#article.state} IS NULL THEN a.state ELSE :#{#article.state} END " +
            "where a.id = :#{#article.id}")
    int update(@Param("article")Article article);

    Page<Article> findByStateAndTypeIn(Pageable pageable, int state, List<Integer> types);

    @Query(value = "SELECT * FROM article WHERE id= ?1", nativeQuery = true)
    List<Article> findByEndTimeAndRegion(int artId);

    @Query(value = "SELECT * FROM article WHERE is_exhibition= 1", nativeQuery = true)
    List<Article> getHomePage();
}
